/* fat.h
 * 
 * Copyright (C) 2010 Ricardo R. Massaro
 *
 * See documentation in the file 'fat.txt'.
 */

#ifndef FAT_H_FILE
#define FAT_H_FILE

typedef void (*fat_func_error)(const char *msg, const char *subject, void *user_data);
typedef int (*fat_func_add_file)(const char *file, int first_cluster, int n_clusters, void *user_data);

typedef struct FAT_INFO {
  unsigned int num_sectors;
  int num_clusters;
  unsigned int sectors_per_fat;
  unsigned int volume_id;
  unsigned int wasted_sectors;

  int num_free_clusters;
  int next_free_cluster;
  unsigned char bpb[512];
  unsigned char fsi[512];
  unsigned char *fat;

  fat_func_error func_error;
  fat_func_add_file func_add_file;
  void *user_data;
} FAT_INFO;

int fat_init(FAT_INFO *fi, unsigned long long disk_size, fat_func_error error, fat_func_add_file add_file, void *user_data);
void fat_free(FAT_INFO *fi);
unsigned char *fat_build_filesystem(FAT_INFO *fi, const char *src_dir, int *n_dir_clusters);

void *fat_get_bpb(FAT_INFO *fi);
void *fat_get_fsi(FAT_INFO *fi);
void *fat_get_fat(FAT_INFO *fi, unsigned int *p_size);

int fat_num_clusters(FAT_INFO *fi);
int fat_num_used_clusters(FAT_INFO *fi);
unsigned int fat_fat_size(FAT_INFO *fi);
unsigned int fat_cluster_size(FAT_INFO *fi);
unsigned int fat_first_cluster_offset(FAT_INFO *fi);
unsigned long long fat_disk_size(FAT_INFO *fi);

#endif /* FAT_H_FILE */

